package com.wm.android.mydemos.activities.mediaplayerexample;

import java.io.File;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.util.Iterator;

import org.farng.mp3.MP3File;
import org.farng.mp3.id3.AbstractID3v2;
import org.farng.mp3.id3.AbstractID3v2Frame;
import org.farng.mp3.id3.AbstractID3v2FrameBody;

import com.wm.android.mydemos.R;

import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.VideoView;

public class LocalMusicFragment extends Fragment {

	private MediaPlayer mPlayer;
	private SeekBar mBar;
	private ImageView mMusicCover;
	private VideoView mVideoScreen;

	private boolean mPlayFlag;
	private int mCurrentPosition;

	private Uri mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/Download/nph.mp3");

	private Handler mHandler = new Handler();

	private Runnable mUpdateSeekBarRunnable = new Runnable() {

		@Override
		public void run() {

			mCurrentPosition = mPlayer.getCurrentPosition();
			mBar.setProgress(mCurrentPosition);
			mHandler.postDelayed(mUpdateSeekBarRunnable, 100);
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View mView = inflater.inflate(R.layout.mediaplayerexample_fragment, container, false);

		mBar = (SeekBar) mView.findViewById(R.id.mediaplayerexample_seekbar);

		mMusicCover = (ImageView) mView.findViewById(R.id.musiccover);
		mMusicCover.setVisibility(View.VISIBLE);
		Bitmap mID3Drawable = createAlbumThumbnail(mUri.toString());
		BitmapDrawable mDrawable = new BitmapDrawable(getResources(), mID3Drawable);
		mMusicCover.setBackgroundDrawable(mDrawable);

		mVideoScreen = (VideoView) mView.findViewById(R.id.videoscreen);
		mVideoScreen.setVisibility(View.GONE);

		Button mLocalMusicStartButton = (Button) mView.findViewById(R.id.mediaplayerexample_button_start);
		mLocalMusicStartButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					if (!mPlayFlag) {
						mPlayer = MediaPlayer.create(getActivity(), mUri);
						mCurrentPosition = 0;
						mBar.setMax(mPlayer.getDuration());
					}
//					use create, prepare is unnecessary
//					mPlayer.prepare();
					mPlayer.seekTo(mCurrentPosition);
					mPlayer.start();
					mHandler.post(mUpdateSeekBarRunnable);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		Button mLocalMusicPauseButton = (Button) mView.findViewById(R.id.mediaplayerexample_button_pause);
		mLocalMusicPauseButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				mPlayer.pause();
				mPlayFlag = true;
				mHandler.removeCallbacks(mUpdateSeekBarRunnable);
			}

		});

		Button mLocalMusicStopButton = (Button) mView.findViewById(R.id.mediaplayerexample_button_stop);
		mLocalMusicStopButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				mPlayer.stop();
				mPlayFlag = false;
				mHandler.removeCallbacks(mUpdateSeekBarRunnable);
			}
		});

		return mView;
	}

	private Bitmap createAlbumThumbnail(String musicPath) {

		Bitmap mBitmap = null;
		try {
			File sourceFile = new File(musicPath);
			MP3File mp3file = new MP3File(sourceFile);

			AbstractID3v2 tag = mp3file.getID3v2Tag();
			Iterator<AbstractID3v2Frame> mIterator = tag.getFrameIterator();
			AbstractID3v2Frame mFrame = null;
			while (mIterator.hasNext()) {
				mFrame = (AbstractID3v2Frame) mIterator.next();
				if (mFrame.getIdentifier().indexOf("APIC") != -1)
					break;
			}
			AbstractID3v2FrameBody body = (AbstractID3v2FrameBody) mFrame.getBody();
			byte[] b = (byte[]) body.getObject("Picture Data");
			mBitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mBitmap;
	}
}
